<template>
  <div>
    <h2>随机点名</h2>
    <div class="box">
      <span>名字是：</span>
      <div class="qs">{{ qs }}</div>
    </div>
    <div class="btns">
      <button @click="startName" class="start">开始</button>
      <button @click="endName" class="end">结束</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: ["马超", "黄忠", "赵云", "关羽", "张飞"],
      qs: "这里显示的是姓名",
      id: "",
      jianname: "",
    };
  },
  methods: {
    startName() {
      clearInterval(this.id);
      this.id = setInterval(this.fn, 100);
    },
    fn() {
      let random = Math.floor(Math.random() * (this.arr.length - 0));
      this.qs = this.arr[random];
      this.jianname = this.arr[random];
    },
    endName() {
      clearInterval(this.id);
      this.qs.forEach((item, index) => {
        if (item == this.jianname) {
          this.qs.splice(index, 1);
        }
        if (this.name.length == 0) {
          this.word = "已经没有数据";
        }
      });
    },
  },
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}

h2 {
  text-align: center;
}

.box {
  width: 600px;
  margin: 50px auto;
  display: flex;
  font-size: 25px;
  line-height: 40px;
}

.qs {
  width: 450px;
  height: 40px;
  color: red;
}

.btns {
  text-align: center;
}

.btns button {
  width: 120px;
  height: 35px;
  margin: 0 50px;
}
</style>